from rest_framework import permissions

from apps.users.models import SystemAccounts
from apps.users.types import ProviderStatusTypeChoices


class IsProvider(permissions.BasePermission):

    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and user.is_provider)


class IsOwnProvider(permissions.BasePermission):

    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and request.user.is_provider)

    def has_object_permission(self, request, view, obj):
        return obj.id == request.user.pk


class IsObjectProvider(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and request.user.is_provider)

    def has_object_permission(self, request, view, obj):
        return obj.created_by.id == request.user.pk


class IsProviderUpdateInvoice(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        data = request.data
        permission_fields = [
            'cancelation_balance',
            'ethera_credit',
            'promo_credit'
        ]
        for field in data:
            if user.is_provider and field in permission_fields:
                return False
        return True


class IsProviderClient(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and user.is_provider)

    def has_object_permission(self, request, view, obj):
        return request.user.id == obj.user_provider.id


class IsOwnClient(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        return request.user.id == obj.id


class IsServiceAccount(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and (request.user.is_welcome_board or request.user.is_kiosk))


class IsWelcomeBoardAccount(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        sys_account = SystemAccounts.objects.filter(id=user.id).first()
        return bool(sys_account and sys_account.id and sys_account.is_welcome_board)


class IsKioskAccount(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and request.user.is_kiosk)


class IsActiveProvider(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        if user.id and user.is_provider:
            if int(user.provider.status) == ProviderStatusTypeChoices.ACTIVE.value:
                return True
        return False


class IsClient(permissions.BasePermission):

    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and request.user.is_client)


class IsSelf(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        return obj.pk == request.user.pk


class CustomPermissions(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        return IsOwnClient().has_object_permission(request, view, obj) or \
            IsProviderClient().has_object_permission(request, view, obj) or \
            permissions.IsAdminUser().has_permission(request, view)


class IsKioskAccount(permissions.BasePermission):
    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.id and request.user.is_kiosk)
